package com.flow.manager.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.flow.model.UserBasic;
import com.flow.model.UserRole;
import com.flow.service.IUserBasicService;
import com.flow.service.IUserRoleService;

public class UserRoleAction {

	private IUserRoleService<UserRole> urService;
	
	private IUserBasicService<UserBasic> ubService;
	
	private List<Integer> checkedStatus = new ArrayList<Integer>();

	public String loadUserRoleInfo() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			
			String ubId = request.getParameter("ubId");
			UserBasic ub = this.ubService.findUserBasicById(ubId);
			List<UserRole> urList = this.urService.findUserRoleById(ubId);
			String rtId = "";
			
			if (urList != null) {
				for (UserRole userRole : urList) {
					rtId = rtId + userRole.getRoleTab().getRtId() + ",";
				}
				request.setAttribute("rtId", rtId);
				request.setAttribute("urList", urList);
			}
			
			request.setAttribute("userBasic", ub);
			return null;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}

		return "ERROR";
	}

	public String updateUserRole() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("rtId");
		String ubId = request.getParameter("ubId");
		try {
			this.urService.updateUserRole(id, this.checkedStatus, ubId);

			return "SUCCESS";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERROR";
	}

	public String findRolePermission() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			String id = request.getParameter("ubId");
			List<UserRole> list = this.urService.findUserRoleById(id);
			request.setAttribute("list", list);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERROR";
	}

	public List<Integer> getCheckedStatus() {
		return checkedStatus;
	}

	public void setCheckedStatus(List<Integer> checkedStatus) {
		this.checkedStatus = checkedStatus;
	}

	public void setUrService(IUserRoleService<UserRole> urService) {
		this.urService = urService;
	}

	public void setUbService(IUserBasicService<UserBasic> ubService) {
		this.ubService = ubService;
	}

}
